/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tal.drivechain.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.KeyListener;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import javax.swing.text.JTextComponent;

/**
 *
 * @author Tal Eisenberg
 */
public class GUITextBox extends JPanel {
    JTextComponent text;
    JScrollPane scroll;
    boolean area;
    int rows = 1, cols;
    Color color;
    private boolean negative;

    public void setColor(Color c) {
        if (c==null) return;
        color = c;
        if (text==null) return;

        if (area) {
            scroll.getHorizontalScrollBar().setUI(new ForkScrollBarUI(color, color.darker()));
            scroll.getVerticalScrollBar().setUI(new ForkScrollBarUI(color, color.darker()));
            scroll.setBorder(new LineBorder(color.darker(), 1));
        } else {
            text.setBorder(new LineBorder(color.darker(), 1));
        }
        setTextColors();
    } public Color getColor() { return color; }

    @Override
    public void setFont(Font f) {
        super.setFont(f);
        if (text!=null) text.setFont(f);
    }

    public void setColumns(int cols) {
        this.cols = cols;
        if (text!=null) resetTextComponent();
    } public int getColumns() { return cols; }

    public void setRows(int rows) {
        this.rows = rows;
        if (text!=null) resetTextComponent();
    } public int getRows() { return rows; }

    public void setText(String val) {
        text.setText(val);
    } public String getText() { return text.getText(); }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        text.setEnabled(enabled);
    }

    public void setNegative(boolean negative) {
        this.negative = negative;
        setTextColors();
    }

    public void setWordWrap(boolean wrap) {
        if (area) {
            ((JTextArea)text).setLineWrap(wrap);
            if (wrap)
                this.scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            else this.scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        }
    } public boolean getWordWrap() {
        if (area)
            return ((JTextArea)text).getLineWrap();
        else return false;
    }

    public void setTabSize(int size) {
        if (area) ((JTextArea)text).setTabSize((Integer)size);
    } public int getTabSize() {
        if (area)
            return ((JTextArea)text).getTabSize();
        else return -1;
    }

    public void setEditable(boolean editable) {
        text.setEditable(editable);
    } public boolean isEditable() { return text.isEditable(); }

    public void initTextComponent() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        if (rows>1) {
            area = true;
            text = new JTextArea(rows, cols);
            scroll = new JScrollPane(text);
            JPanel blackPanel = new JPanel();
            blackPanel.setBackground(Color.BLACK);
            scroll.setCorner(JScrollPane.LOWER_TRAILING_CORNER, blackPanel);
            scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            if (getWordWrap())
                this.scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            else this.scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

            this.add(scroll);
        } else {
            area = false;
            text = new JTextField(cols);
            this.add(text);
        }
        text.setMargin(new Insets(1, 2, 1, 2));
        for (KeyListener l : this.getKeyListeners()) {
            text.addKeyListener(l);
        }
        text.setToolTipText(this.getToolTipText());
    }

    public void resetTextComponent() {
        boolean wordWrap = getWordWrap();
        int tabSize = getTabSize();
        String stext = getText();
        boolean editable = isEditable();

        this.removeAll();
        initTextComponent();
        setWordWrap(wordWrap);
        setTabSize(tabSize);
        setText(stext);
        setEnabled(isEnabled());
        setFont(getFont());
        setNegative(negative);
        setEditable(editable);
        setColor(getColor());
        this.doLayout();
    }

    private void setTextColors() {
        if (text==null || color==null) return;
        
        text.setCaretColor(color.darker());
        text.setSelectionColor(color.darker());
        if (!negative) {
            text.setBackground(color);
            text.setForeground(Color.BLACK);
        } else {
            text.setBackground(Color.BLACK);
            text.setForeground(color);
        }
    }

    public JTextComponent getTextComponent() {
        return text;
    }

    @Override
    public synchronized void addKeyListener(KeyListener l) {
        super.addKeyListener(l);
        text.addKeyListener(l);
    }

    @Override
    public synchronized void removeKeyListener(KeyListener l) {
        super.removeKeyListener(l);
        text.removeKeyListener(l);
    }

    @Override
    public void setToolTipText(String string) {
        super.setToolTipText(string);
        if (text!=null) text.setToolTipText(string);
    }
}
